﻿using System;
using System.Threading;
using System.Diagnostics;
using T0yK4T.Cryptography;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using T0yK4T.Tools;
using T0yK4T.IO;

namespace TestProject
{
    class Program
    {
#if DEBUG
        const int pktCount = 1024;// * 32;
#else
        const int pktCount = 1024 * 32;
#endif
        public const string message = "Hello World! This is a sample message with some extra text added, just to mess with your head... Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

        static void WriteSelection()
        {
            //Console.WriteLine(message.Length);
            uint val = 0;
            uint val2 = val++;
            Console.WriteLine(val);
            Console.WriteLine(val2);
            Console.WriteLine("Press 1 to test RSAHelper");
            Console.WriteLine("Press 2 to test Prng");
            Console.WriteLine("Press 3 to test Handshaker");
            Console.WriteLine("Press 4 to test MongoDB");
            Console.WriteLine("Press 5 to test New CryptoConnection");
            Console.WriteLine("Press 6 to test Serializer");
            Console.WriteLine("Press 7 to test Encryption");
            Console.WriteLine("Press 8 to test Encrypted Communications");
            Console.WriteLine("Press 9 to test ToyChat MongoDB Handler");
            Console.WriteLine();
            Console.WriteLine("Press 0 to run the test UI");
            Console.WriteLine();
            Console.WriteLine("Press Escape or Q to exit");
            Console.Write("Select: ");
        }

        static void Main(string[] args)
        {
            RSAHelper.GenKeys();
            GC.Collect();
            Console.SetBufferSize((int)(Console.LargestWindowWidth / 1.5), 2048);
            Console.SetWindowSize((int)(Console.LargestWindowWidth / 1.5), (int)(Console.LargestWindowHeight / 1.5));
            WriteSelection();
            ConsoleKeyInfo key;
            while ((key = Console.ReadKey()).Key != ConsoleKey.Escape && key.Key != ConsoleKey.Q)
            {
                Console.Clear();
                switch (key.Key)
                {
                    case ConsoleKey.D1:
                        TestRSA();
                        break;
                    case ConsoleKey.D2:
                        TestPrng();
                        break;
                    case ConsoleKey.D3:
                        new HandshakerTest().Run();
                        break;
                    case ConsoleKey.D4:
                        new MongoDBTest().Run();
                        break;
                    case ConsoleKey.D5:
                        new CryptoConnectionTest().Run();
                        break;
                    case ConsoleKey.D6:
                        new SerializerTest().Run();
                        break;
                    case ConsoleKey.D7:
                        new EncryptionTest().Run();
                        break;
                    case ConsoleKey.D8:
                        new CryptoCommunicationTest().Run();
                        break;
                    case ConsoleKey.D9:
                        new ToyMongoDBTest().Run();
                        break;
                    case ConsoleKey.D0:
                        StartUI();
                        break;
                }
                Console.Clear();
                WriteSelection();
            }
        }

        const int threadcount = 200;

        [STAThread]
        static void StartUI()
        {
            System.Windows.Forms.Application.EnableVisualStyles();
            try { System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); }
            catch { }
            System.Windows.Forms.Application.Run(new Mainform());
        }

        static void TestPrng()
        {
            const int count = 1024;
            Console.WriteLine("Dumping {0} random numbers from CryptoComming.GetPrngInt()", count);
            Console.WriteLine("Press return to begin");
            Console.ReadLine();
            for (int i = 0; i < count; i++)
                Console.WriteLine(CryptoCommon.GetPrngInt());
            Console.WriteLine("Done...");
            Console.ReadLine();
        }

        static void TestRSA()
        {
            RSAHelper helper = new RSAHelper(new ConsoleLogger());
            string message = "Hello World";
            byte[] data = ASCIIEncoding.ASCII.GetBytes(message);
            string str = helper.EncryptToBase64String(data);
            byte[] newData = helper.DecryptBase64String(str);
            string newMessage = ASCIIEncoding.ASCII.GetString(newData);
            Console.WriteLine("Message1: {0}", message);
            Console.WriteLine("Message2: {0}", newMessage);
            Console.ReadLine();
        }
    }
}
